﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution437
    {
        public int res = 0;

        public int PathSum(TreeNode root, int targetSum)
        {
            DFS(root, targetSum);
            return res;
        }

        private void DFS(TreeNode root, int targetSum)
        {
            if (root == null) return;
            MyDFS(root, targetSum);

            DFS(root.left, targetSum);
            DFS(root.right, targetSum);
        }

        private void MyDFS(TreeNode root, int targetSum)
        {
            Console.WriteLine(targetSum);
            //收集当前顶点
            targetSum -= root.val;
            if (targetSum == 0)
            {
                res++;
            }
            if (root.left != null)
            {
                MyDFS(root.left, targetSum);
            }
            if (root.right != null)
            {
                MyDFS(root.right, targetSum);
            }
        }
    }
}